package com.situ.java.view;

import com.situ.java.domain.Book;
import com.situ.java.domain.BorrowedRd;
import com.situ.java.domain.Person;
import com.situ.java.service.BookListService;
import com.situ.java.service.BorrowedListService;
import com.situ.java.service.MyException;
import com.situ.java.service.PersonListService;

import java.io.*;
import java.util.Collections;
import java.util.List;

//        1-进书、办理借书卡
//        2-借书和还书功能
//        3-查询某书的借阅记录
//        4-查询谋卡的借阅记录
//        5-按借阅次数，显示图书信息。
public class MainView implements Serializable{
	/** serialVersionUID*/  
    private static final long serialVersionUID = 2384384681L;
    
    private BookListService bookSvc = new BookListService();
	private PersonListService personSvc = new PersonListService();
	private BorrowedListService bordSvc = new BorrowedListService();

	public void enterMainMenu() {

		boolean loopFlag = true;
		char menu = 0;

		while (loopFlag) {
			System.out.println("1-图书列表                                   2-添加图书");
			System.out.println("3-借书卡列表                               4-办理借书卡");
			System.out.println("5-借书                                           6-还书");
			System.out.println("7-查询图书借阅记录                    8-查询借书卡借阅记录");
			System.out.println("9-按借阅次数显示图书列表         0-退出");
			System.out.println("请选择(0-9)：");
			menu = TSUtility.readMenuSelection();

			switch (menu) {
			case '1':
				getBooks();
				break;
			case '2':
				addBook();
				break;
			case '3':
				getPersons();
				break;
			case '4':
				addPerson();
				break;
			case '5':
				borrowedBook();
				break;
			case '6':
				returnBook();
				break;
			case '7':
				viewBook();
				break;
			case '8':
				viewPerson();
				break;
			case '9':
				getBookList();
				break;
			case '0':
				System.out.println("确认是否退出Y/N");
				char isExit = TSUtility.readConfirmSelection();
				if (isExit == 'Y') {
					loopFlag = false;
				}
				break;
			default:
			    break;
			}
		}
	}

	private void getBookList() {
		System.out.println("---------------------按借阅次数显示图书列表---------------------");
		List<Book> books = bookSvc.getBookList();
		Collections.sort(books);
		System.out.println("ID\t书名\t作者\t出版社\t状态\t借阅次数");
		books.forEach(param -> {
			System.out.println(param);
		});

	}

	private void viewPerson() {
		System.out.println("---------------------查询借书卡借阅记录---------------------");
		System.out.print("请输入要查询的借书卡的ID：");
		int personId = TSUtility.readInt(); // 非空
		List<BorrowedRd> bords = bordSvc.getPersonBords(personId);
		System.out.println("书ID\t借书卡ID\t借出时间\t归还时间");
		for (int i = 0; i < bords.size(); i++) {
			String temp = bords.get(i).getBookID() + "\t\t" + bords.get(i).getPersonID() + "\t"
					+ bords.get(i).getBorrowTime().getTime();
			if (bords.get(i).getReturnTime() != null) {
				temp += bords.get(i).getReturnTime().getTime();
			}
			System.out.println(temp);
		}

	}

	private void viewBook() {
		System.out.println("---------------------查询图书借阅记录---------------------");
		System.out.print("请输入要查询的图书的ID：");
		int bookId = TSUtility.readInt(); // 非空
		List<BorrowedRd> bords = bordSvc.getBookBords(bookId);
		System.out.println("书ID\t借书卡ID\t借出时间\t归还时间");
		for (int i = 0; i < bords.size(); i++) {
			String temp = bords.get(i).getBookID() + "\t\t" + bords.get(i).getPersonID() + "\t"
					+ bords.get(i).getBorrowTime().getTime();
			if (bords.get(i).getReturnTime() != null) {
				temp += bords.get(i).getReturnTime().getTime();
			}
			System.out.println(temp);
		}
	}

	private void returnBook() {
		System.out.println("---------------------借书---------------------");
		System.out.print("请输入要还的图书的ID：");
		int bookId = TSUtility.readInt(); // 非空
		try {
			bordSvc.returnBook(bookId);
			System.out.println("还书成功");
			// 回车继续
			TSUtility.readReturn();
		} catch (MyException e) {
			System.out.println("还书失败，原因： " + e.getMessage());
			// 回车继续
			TSUtility.readReturn();
		}

	}

	private void borrowedBook() {
		System.out.println("---------------------借书---------------------");
		System.out.print("请输入要借的图书的ID：");
		int bookId = TSUtility.readInt(); // 非空
		System.out.print("请输入您的借书卡ID：");
		int personId = TSUtility.readInt(); // 非空
		try {
			bordSvc.borrowedBook(bookId, personId);
			System.out.println("借书成功");
			// 回车继续
			TSUtility.readReturn();
		} catch (MyException e) {
			System.out.println("借书失败，原因： " + e.getMessage());
			// 回车继续
			TSUtility.readReturn();
		}
	}

	// 遍历图书
	private void getBooks() {
		Book[] books = bookSvc.getBooks();
		System.out.println("ID\t书名\t作者\t出版社\t状态\t借阅次数");
		for (int i = 0; i < books.length; i++) {
			System.out.println(books[i]);
		}
	}

	// 遍历借书卡
	private void getPersons() {
		Person[] persons = personSvc.getPersons();
		System.out.println("ID\t姓名");
		for (int i = 0; i < persons.length; i++) {
			System.out.println(persons[i]);
		}
	}

	// 进书
	private void addBook() {
		System.out.println("---------------------进书---------------------");
		System.out.print("请输入要添加的图书ID：");
		int id = TSUtility.readInt(); // 非空
		System.out.print("请输入要添加的图书名：");
		String name = TSUtility.readKeyBoard(34, false); // 非空
		System.out.print("请输入要添加的图书作者(选填)：");
		String author = TSUtility.readKeyBoard(34, true); // 可空
		System.out.print("请输入要添加的图书出版社(选填)：");
		String press = TSUtility.readKeyBoard(34, true); // 可空

		try {
			Book b = new Book(id, name, author, press);
			bookSvc.addBook(b);
			System.out.println("添加成功");
			// 回车继续
			TSUtility.readReturn();
		} catch (MyException e) {
			System.out.println("添加失败，原因： " + e.getMessage());
			TSUtility.readReturn();
		}
	}

	// 办理借书卡
	private void addPerson() {
		System.out.println("---------------------办理借书卡---------------------");
		System.out.print("请输入要办理的借书卡ID：");
		int id = TSUtility.readInt(); // 非空
		System.out.print("请输入要办理借书卡的用户名：");
		String name = TSUtility.readKeyBoard(34, false); // 非空

		try {
			Person p = new Person(id, name);
			personSvc.addPerson(p);
			System.out.println("添加成功");
			// 回车继续
			TSUtility.readReturn();
		} catch (MyException e) {
			System.out.println("添加失败，原因： " + e.getMessage());
			TSUtility.readReturn();
		}
	}

	public static void main(String[] args) throws FileNotFoundException, IOException {
		MainView view = null;
		
	    // 序列化
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Object.dat"));
            view = (MainView) ois.readObject();
            ois.close();
        } catch (ClassNotFoundException e) {
            System.out.println("文件不匹配");
        } catch (IOException e) {
            System.out.println();
        }
        
		if (view == null) {
		    view = new MainView();
        }
		view.enterMainMenu();

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Object.dat"));
        oos.writeObject(view);
        oos.flush();
        oos.close();
	}

}
